--
-- Author: yawei.zhu
-- Date: 2018-12-17 17:14:30
--
-- PvpChallengeNoticeView
--
local NewBoxTipsView = require("app.newBag.views.NewBoxTipsView")
local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpChallengeNoticeScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		self:closeView()
	end)

	self._ui["Panel/Panel_content/Panel_bom/gift"]:setTouchEnabled(true)
	self._ui["Panel/Panel_content/Panel_bom/gift"]:addClickEventListener(function(sender)
		self:_showRareInfo(sender)
	end)

	-- 随机效果 暂时不做
	self._ui["Panel/Panel_content/Panel_top/text"]:setString("")

	self._ui["Panel/Panel_content/Panel_icon/tlt"]:setString(L("challengeTips16"))
	self._ui["Panel/Panel_content/Panel_bom/tlt"]:setString(L("lua_code_text_332"))

	-- 奖励
	self._iconRenders = {}
	local iconPanel = self._ui.Panel_icon
	for i = 1, 5 do
		local render = IconRender.create(iconPanel:getChildByName("icon_"..i))
		render:setTipEnable(true)
		self._iconRenders[i] = render
	end
end

function ClassRef:updateView(challengeInfo)
	self._challengeInfo = challengeInfo
	self._ui["Panel/Panel_content/Panel_bom/num"]:setString(challengeInfo.lv)

	local challengeConf = GD:getChallengeInfo(challengeInfo.playId)
	local szTitle = string.format(L("lua_code_text_479"), L(challengeConf.targetDesc))
	self._ui["Panel/Panel_content/Panel_top/tlt"]:setString(szTitle)

	-- 设置背景图片
	self._ui["Panel/Panel_content/Panel_top/bg"]:loadTexture(string.format(Res.ChallengeBgPath, challengeConf.icon))

	-- 设置奖励
	local arrAward = string.split()
	local items = UD:parseItemsFromString(challengeConf.rankRewardShow)
	local count = #items
	local midX = 201
	local intervalX = 78
	local midNum = (count + 1) / 2

	for i = 1, 5 do
		local render = self._iconRenders[i]
		if i <= count then
			render:setVisible(true)
			render:setData(items[i])
			render:setPositionX(midX - (midNum - i ) * intervalX - 38)
		else
			render:setVisible(false)
		end
	end
end

function ClassRef:_showRareInfo(target)

    local dataTemp = {
        name = L("seasonlv_name"),
        desc = L("challengeSeasonTips"),
        notice = L("tips_3000412"),
    }
    local rewards = {}
    local level = self._challengeInfo.lv
    if level > 0 then
        local conf = GD:getChallengeLevelInfo(level)
        if conf then
            local dropIds = string.split(conf.seasonReward, ";")
            for _, dropId in ipairs(dropIds) do
                local rets = UD:parseDropIDToBagItem(dropId)
                if rets then
                    for _, itemData in ipairs(rets) do
                        table.insert(rewards, itemData)
                    end
                end
            end
        end
    end
    dataTemp.rewards = rewards

    local rarePanel = NewBoxTipsView.create()
    rarePanel:setData(dataTemp)
    UIHandler:addPopupUi(rarePanel)
    -- 点击空白区域关闭
    display.nodeRemoveForNotHit(rarePanel, function()
        rarePanel:removeFromParent()
    end)

    local node = target
    local box = node:getBoundingBox()
    local worldPos = node:convertToWorldSpace(cc.p(0, 0))
    local nodeHeight = rarePanel:getContentSize().height

    -- display.adjustNodePos 会把rarePanel默认和box的最上面对齐，这里需要中间对齐，所以就手动设置坐标了
    rarePanel:setPositionX(worldPos.x + box.width)
    rarePanel:setPositionY(worldPos.y + box.height / 2  - nodeHeight / 2)
end

return ClassRef
